<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="@/assets/logo.png" alt="" class="log" />
        <span class="logname">后台管理系统</span>
      </div>
      <el-button type="warning" @click="logout" class="btn_out"
        >退出</el-button
      ></el-header
    >
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏 -->
        <!--opened唯一显示  collapse折叠  transition关闭动漫   router开启路由模式-->
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409Eff"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :default-active="clickMenu"
          router
        >
          <!-- 一级 -->
          <el-submenu
            :index="obj.id + ''"
            v-for="obj in menuList"
            :key="obj.id"
          >
            <template slot="title">
              <i :class="iconsObj[obj.id]"></i>
              <span>{{ obj.authName }}</span>
            </template>
            <!-- 二级 -->
            <el-menu-item
              :index="'/' + item.path"
              v-for="item in obj.children"
              :key="item.id"
              @click="saveMenu('/' + item.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span> {{ item.authName }} </span>
              </template>
            </el-menu-item>

            <!-- <el-submenu index="1-4">
              <template slot="title">选项4</template>
              <el-menu-item index="1-4-1">选项1</el-menu-item>
            </el-submenu> -->
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'home',
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao',
      },
      isCollapse: false,
      clickMenu: '',
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    saveMenu(urlhash) {
      this.clickMenu = urlhash
      window.sessionStorage.setItem('curMenu', urlhash)
    },
    logout() {
      this.$msgBox
        .confirm('确定退出吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
        .then(() => {
          this.$msgBox.confirm('退出成功!')
          window.sessionStorage.removeItem('token')

          this.$router.push('/login')
        })
    },
    async created() {
      const urlhash = window.sessionStorage.getItem('curMenu')
      if (urlhash) this.clickMenu = urlhash
    },
    async getMenuList() {
      const { data: res } = await this.$axios.get('menus')
      // console.log(res)
      // 获取不到就报错
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 获取成功就将数据添加到数据中
      this.menuList = res.data
      // console.log(this.menuList)
    },

    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
  },
}
</script>

<style lang=less>
.home-container {
  height: 100%;
}
.log {
  width: 50px;
  height: 50px;
  padding: 10px;
  border-radius: 9999px;
}
.logname {
  position: absolute;
  margin: 20px;
}
.btn_out {
  height: 40px;
  margin: 10px;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;

  color: #fff;
  font-size: 20px;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  background: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #ffff;
  text-align: center;
  letter-spacing: 5px; /* 文字之间的间距 */
  cursor: pointer; /* 将鼠标改为小手 */
}


</style>